<?php

namespace App\Http\Controllers\Backend\Bank;

use App\Constants\RedisKey;
use App\Http\Requests\Backend\Bank\BankRequest;
use App\Models\AdminBankIcon;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use Overtrue\LaravelPinyin\Facades\Pinyin;

class BankController extends Controller {
    public function __construct() {
        $this->middleware('auth:backend');
        $this->user = auth('backend')->user();
    }

    public function changeConnection(){
        $bank = new AdminBankIcon();
        $bank->setConnection('backend');
        return $bank;
    }
    /**
     * 首页信息
     * @param BankRequest $request
     * @return
     * @author luwei
     * @date 2019-01-06 17:02
     */
    public function index(BankRequest $request) {
        $pageSize = $request->get('page_size', 10);
        $bank = $this->changeConnection();
        $list     = $bank->orderBy('first_word')->paginate($pageSize);
        $info     = [];
        foreach ($list as $v) {
            $info[] = [
                'id'           => $v->id,
                'first_word'   => $v->first_word,
                "bank_name"    => $v->bank_name,
                "bin_num"      => $v->bin_num,
                "created_at"   => $v->created_at->format('Y-m-d H:i:s'),
                "updated_at"   => $v->updated_at->format('Y-m-d H:i:s'),
                "single_limit" => $v->single_limit,
                "day_limit"    => $v->day_limit,
                "path"         => $v->icon_oss_url,
                "icon_oss_url" => $v->ali_oss_full_url
            ];
        }
        return resp(['data' => $info, 'total' => $list->total()]);
    }

    /**
     * 新建银行卡图标信息
     * @param BankRequest $request
     * @return mixed
     * @author luwei
     * @date 2019-01-06 15:57
     */
    public function store(BankRequest $request) {
        $params                 = $this->extractInputFromRules($request, $request->rules());
        $params['single_limit'] = $request->get('single_limit', 0);
        $params['day_limit']    = $request->get('day_limit', 0);
        $params['first_word']   = strtoupper(substr(Pinyin::abbr($params['bank_name']), 0, 1));
        $params['icon_oss_url'] = $params['path'];
        AdminBankIcon::create($params);
        $key = RedisKey::BANK_ICON_LIST;
        Redis::del($key);
        return success([]);
    }

    /**
     * 银行卡图标详情
     * @param BankRequest $request
     * @param $bankId
     * @return mixed
     * @author luwei
     * @date 2019-01-06 16:50
     */
    public function detail(BankRequest $request, $bankId) {
        $bank = $this->changeConnection();
        $bankInfo = $bank->findOrFail($bankId);
        $data     = array(
            'id'           => $bankId,
            'bank_name'    => $bankInfo->bank_name,
            'bin_num'      => $bankInfo->bin_num,
            'path'         => $bankInfo->icon_oss_url,
            'icon_oss_url' => $bankInfo->ali_oss_full_url,
            'single_limit' => $bankInfo->single_limit,
            'day_limit'    => $bankInfo->day_limit
        );
        return success($data);
    }

    /**
     * 更新银行卡图标信息
     * @param BankRequest $request
     * @param $bankId
     * @return mixed
     * @author luwei
     * @date 2019-01-06 17:01
     */
    public function update(BankRequest $request, $bankId) {
        $params                 = $this->extractInputFromRules($request, $request->rules());
        $params['single_limit'] = $request->get('single_limit', 0);
        $params['day_limit']    = $request->get('day_limit', 0);
        $params['first_word']   = strtoupper(substr(Pinyin::abbr($params['bank_name']), 0, 1));
        $params['icon_oss_url'] = $params['path'];
        $bankInfo               = AdminBankIcon::findOrFail($bankId);
        $bankInfo->update($params);
        $key = RedisKey::BANK_ICON_LIST;
        Redis::del($key);
        return success([]);
    }

    /**
     * 删除银行卡图标信息
     * @param BankRequest $request
     * @param $bankId
     * @return mixed
     * @throws \Exception
     * @author luwei
     * @date 2019-01-06 17:18
     */
    public function delete(BankRequest $request, $bankId) {
        $bankInfo = AdminBankIcon::findOrFail($bankId);
        $bankInfo->delete();
        $key = RedisKey::BANK_ICON_LIST;
        Redis::del($key);
        return success([]);
    }

    /**
     * 可用银行卡列表
     * @return mixed
     * @author luwei
     * @date 2019-01-06 16:36
     */
    public function bankList() {
        $bankList     = array_keys(config('bank_code'));
        $adminBankIconModel = $this->changeConnection();
        $bankIconList = $adminBankIconModel->cursor();
        $tmpList      = [];
        foreach ($bankIconList as $k => $v) {
            $tmpList[] = $v->bank_name;
        }
        $diffArray = array_diff($bankList, $tmpList);
        return success(array_values($diffArray));
    }
}
